/*---------------------------------------------------------------------
 * File name: hal_timer.cpp
 *
 * Copyright (c) <2020-2022>, <ChenLong>
 *
 * All rights reserved.
 *
 * Author: ChenLong
 * Email: worldlong@foxmail.com
 *--------------------------------------------------------------------*/
#include "stm32g4xx.h"
#include "hal_system.h"
#include "hal_config.h"
/*
*/
void HAL_System::init()
{
  __HAL_RCC_SYSCFG_CLK_ENABLE();
  __HAL_RCC_PWR_CLK_ENABLE();
  
  //DMA power control  
#if DMA1_ENABLE
  __HAL_RCC_DMAMUX1_CLK_ENABLE();
  __HAL_RCC_DMA1_CLK_ENABLE();
#endif
#if DMA2_ENABLE
  __HAL_RCC_DMAMUX1_CLK_ENABLE();
  __HAL_RCC_DMA2_CLK_ENABLE();
#endif
  
  //GPIO power control  
#if GPIOA_ENABLE
  __HAL_RCC_GPIOA_CLK_ENABLE();
#endif
#if GPIOB_ENABLE
  __HAL_RCC_GPIOB_CLK_ENABLE();
#endif
#if GPIOC_ENABLE
  __HAL_RCC_GPIOC_CLK_ENABLE();
#endif
#if GPIOD_ENABLE
  __HAL_RCC_GPIOD_CLK_ENABLE();
#endif
#if GPIOE_ENABLE
  __HAL_RCC_GPIOE_CLK_ENABLE();
#endif
#if GPIOF_ENABLE
  __HAL_RCC_GPIOF_CLK_ENABLE();
#endif
#if GPIOG_ENABLE
  __HAL_RCC_GPIOG_CLK_ENABLE();
#endif
#if GPIOH_ENABLE
  __HAL_RCC_GPIOH_CLK_ENABLE();
#endif

//TIM power control
#if TIM1_ENABLE
  __HAL_RCC_TIM1_CLK_ENABLE();
#endif
#if TIM2_ENABLE
  __HAL_RCC_TIM2_CLK_ENABLE();
#endif
#if TIM3_ENABLE
  __HAL_RCC_TIM3_CLK_ENABLE();
#endif
#if TIM4_ENABLE
  __HAL_RCC_TIM4_CLK_ENABLE();
#endif
#if TIM5_ENABLE
  __HAL_RCC_TIM5_CLK_ENABLE();
#endif
#if TIM6_ENABLE
  __HAL_RCC_TIM6_CLK_ENABLE();
#endif
#if TIM7_ENABLE
  __HAL_RCC_TIM7_CLK_ENABLE();
#endif
#if TIM8_ENABLE
  __HAL_RCC_TIM8_CLK_ENABLE();
#endif
#if TIM15_ENABLE
  __HAL_RCC_TIM15_CLK_ENABLE();
#endif
#if TIM16_ENABLE
  __HAL_RCC_TIM16_CLK_ENABLE();
#endif
#if TIM17_ENABLE
  __HAL_RCC_TIM17_CLK_ENABLE();
#endif
#if TIM20_ENABLE
  __HAL_RCC_TIM20_CLK_ENABLE();
#endif

//UART power control
#if UART1_ENABLE
  __HAL_RCC_USART1_CLK_ENABLE();
#endif
#if UART2_ENABLE
  __HAL_RCC_USART2_CLK_ENABLE();
#endif
#if UART3_ENABLE
  __HAL_RCC_USART3_CLK_ENABLE();
#endif
#if UART4_ENABLE
  __HAL_RCC_UART4_CLK_ENABLE();
#endif
#if UART5_ENABLE
  __HAL_RCC_UART5_CLK_ENABLE();
#endif
//  
//  //SPI power control
//#if SPI1_ENABLE
//  RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
//#endif
//#if SPI2_ENABLE
//  RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
//#endif
//#if SPI3_ENABLE
//  RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI3, ENABLE);
//#endif
//#if SPI4_ENABLE
//  RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI4, ENABLE);
//#endif
}
/*
*/
void HAL_System::close_all_periph()
{
  interrupt_disable();
  
  for(int i = 0; i < 8; i++) 
  {
    NVIC->ICER[i] = 0xFFFFFFFF; // Disable interrupt
    NVIC->ICPR[i] = 0xFFFFFFFF; // Clear interrupt flag
  }
  HAL_RCC_DeInit();
}
/*
*/
void HAL_System::reset()
{
  close_all_periph();
  NVIC_SystemReset();
}




